//var httpconf = {
//    host: 'http://127.0.0.1:',
//    port: 50134,
//    root: '/WorkAssistant-api/api/v1'
//};
var httpconf = {
    //host: 'http://192.168.6.101:',
    //host: 'http://192.168.5.132:',
    host: 'http://202.91.248.189:',
    port: 5858,
    //port:8080,
    root: '/WorkAssistant-api/api/v1'
};
//var httpconf = {
//    host: 'http://192.168.6.74:',
//    port: 8080,
//    root: '/WorkAssistant/api/v1'
//};
//var httpconf = {
//    host: 'http://10.147.251.212:',
//    port: 10104,
//    root:'/WorkAssistant-api/api/v1'
//};

var apiurl = httpconf.host + httpconf.port + httpconf.root;


var tiankuang_apiconf = {

    //node代理接口配置
    //host: 'http://127.0.0.1:',
    //port: 50136,
    //root: '/moa-server/app/'

    host: 'http://202.91.248.189:',
    port: 5859,
    root: '/moa-server/app/'
    //host: 'http://192.168.5.132:',
    //port: 8081,
    //root: '/moa-server/app/'

    //host: 'http://192.168.3.101:',
    //port: 8080,
    //root: '/moa-server/app/'

    //host: 'http://10.147.251.212:',
    //port: 10106,
    //root: '/moa-server/app/'
};
//天宽apiurl
var tk_apiurl = tiankuang_apiconf.host + tiankuang_apiconf.port + tiankuang_apiconf.root;

//创建人:于丹
//用户信息
var token, user_id, group_id, user_name, imToken, qyxBindId, role, deptName;
$(function () {
    $.getUrlParam = function (name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
        var r = window.location.search.substr(1).match(reg);
        if (r != null) return decodeURI(r[2]);
        return null;
    };

    //进入页面,token
    token = $.getUrlParam('token');
    user_id = $.getUrlParam('userId');
    group_id = $.getUrlParam('team_id');
    user_name = $.getUrlParam('user_name');
    imToken = $.getUrlParam('imToken');
    qyxBindId = $.getUrlParam('qyxBindId');
    role = $.getUrlParam('role');
    deptName = $.getUrlParam('deptName');

    if (token != null && user_id != null && group_id != null) {
        localStorage.setItem("token", token);
        localStorage.setItem("user_id", user_id);
        localStorage.setItem("group_id", group_id);
        localStorage.setItem("user_name", user_name);
        localStorage.setItem("imToken", imToken);
        localStorage.setItem("qyxBindId", qyxBindId);
        localStorage.setItem("role", role);
        localStorage.setItem("deptName", deptName);
    }

    token = localStorage.getItem("token") == (undefined || null) ? 0 : localStorage.getItem("token");
    user_id = localStorage.getItem("user_id") == (undefined || null) ? 0 : localStorage.getItem("user_id");
    group_id = localStorage.getItem("group_id") == (undefined || null) ? 0 : localStorage.getItem("group_id");
    user_name = localStorage.getItem("user_name") == (undefined || null) ? 0 : localStorage.getItem("user_name");
    imToken = localStorage.getItem("imToken") == (undefined || null) ? 0 : localStorage.getItem("imToken");
    qyxBindId = localStorage.getItem("qyxBindId") == (undefined || null) ? 0 : localStorage.getItem("qyxBindId");
    role = localStorage.getItem("role") == (undefined || null) ? 0 : localStorage.getItem("role");
    deptName = localStorage.getItem("deptName") == (undefined || null) ? 0 : localStorage.getItem("deptName");
});

//时间
var tempTime = {};
var min_sec;            //分:秒
var whole_time;         //2016.10.26 12:33
var hours_min;          //时:分
var whole_day;          //2016.10.26
var mon_day;
var mon_day_cn;          //10月26日
var mon_day_en;          //10-26
var year_mon_day_cn;       //2016年10月26日
var year_mon_day_en;       //2016-10-26
var day_min_cn;            //10月26日 12:33
var day_min_en;            //10-26 12:33
var timestamp;           //时间戳

function initDate(date) {
    tempTime = {
        year: date.getFullYear(),
        month: (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1),
        day: date.getDate() < 10 ? '0' + date.getDate() : date.getDate(),
        hours: date.getHours() < 10 ? '0' + date.getHours() : date.getHours(),
        min: date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes(),
        sec: date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds(),
    };
    whole_time = tempTime.year + "." + tempTime.month + "." + tempTime.day + " " + tempTime.hours + ":" + tempTime.min;
    whole_day = tempTime.year + "." + tempTime.month + "." + tempTime.day;
    hours_min = tempTime.hours + ":" + tempTime.min;
    min_sec = tempTime.min + ":" + tempTime.sec;
    mon_day_cn = tempTime.month + "月" + tempTime.day + "日";
    mon_day_en = tempTime.month + "-" + tempTime.day;
    year_mon_day_cn = tempTime.year + "年" + tempTime.month + "月" + tempTime.day + "日";
    year_mon_day_en = tempTime.year + "-" + tempTime.month + "-" + tempTime.day;
    day_min_cn = tempTime.month + "月" + tempTime.day + "日" + " " + tempTime.hours + ":" + tempTime.min;
    day_min_en = tempTime.month + "-" + tempTime.day + " " + tempTime.hours + ":" + tempTime.min;
    mon_day = tempTime.month + "月" + tempTime.day + "日";
}

/*创建人:于丹
 创建时间:2016年11月7号 11:11
 方法功能:提示框
 参数:1,obj:弹出框对象;2,提示的信息
 * */
function pop_up_fn(obj, title) {
    obj.text(title);
    obj.fadeIn();
    obj.fadeOut(3000);
}

/*创建人:于丹
 创建时间:2016年11月7号 11:11
 //四舍五入,保留一位小数
 */
function fomatFloat(src, pos) {
    return Math.round(src * Math.pow(10, pos)) / Math.pow(10, pos);
}

//解析数据
//创建人:于丹
//创建时间:周一下午3点32分
function data_analysis(d) {
    var data = '';
    if (typeof d == "object") {
        data = d;
    }
    else {
        data = eval('(' + d + ')');
        if (data.result) {
            data.result = eval('(' + data.result + ')');
        }
    }
    return data;
}

//点击分享
function shareContent(attId, team, content, title, applicationId, applicationType, attSize, hyperlink) {
    var share;
    share = document.createElement('iframe');
    share.style.display = 'none';
    document.documentElement.appendChild(share);

    var jsonString = {};
    jsonString.attId = attId;
    if (team == null) {
        team = "";
    }
    jsonString.team = team;
    jsonString.content = content;
    jsonString.title = title;
    jsonString.applicationId = applicationId;
    jsonString.applicationType = applicationType;
    jsonString.attSize = attSize;
    jsonString.hyperlink = hyperlink;

    //share.src = "/share_to_anDroid_UI?attId="+attId+"&team="+team+"&content="+content+"&title="+title+"&applicationId="+applicationId+"&applicationType="+applicationType+"&attSize="+attSize+"&hyperlink="+hyperlink+"";
    share.src = "/share_to_anDroid_UI?jsonString=" + JSON.stringify(jsonString) + "";

}

/**
 * 提取背景色
 * 创建人:吴越
 * 周五 11:09
 */
var bgColorList = ["#5ec9f6",
    "#78919d",
    "#99a4c7",
    "#a1887f",
    "#9a89b9",
    "#6bb5ce",
    "#f6bf25",
    "#fe943d",
    "#f95b8b",
    "#669ae6",
    "#f45f5d",
    "#ba85cb",
    "#77c16c",
    "#3ac1b4",
    "#c5cb64",
    "#35c6bc1"];
function avatar_getColor(qyxBindId) {
    if (qyxBindId == "" || qyxBindId == undefined || qyxBindId == "undefined") {
        bgColor = bgColorList[1];
    } else {
        var bgColor = qyxBindId.substring(qyxBindId.length - 1);
        bgColor = bgColorList[bgColor];
    }
    return bgColor;
}
/**
 * 获取用户头像
 * @param userId
 * @param token
 * @param avatar_id
 * @returns url
 */
function avatar_getUrl(userId, token, avatar_id) {
    var get_url = "http://122.224.88.220:8090/moa-server/app/" +
        "att/download?userId=" + userId + "&token=" + token + "&data=" + avatar_id + "";
    //var get_url = "http://10.147.251.212:10102/moa-server/app/" +
    //    "att/download?userId=" + userId + "&token=" + token + "&data=" + avatar_id + "";
    //var get_url = "http://127.0.0.1:50134/moa-server/app/" +
    //    "att/download?userId=" + userId + "&token=" + token + "&data=" + avatar_id + "";

    //var file = $('#banner-pic-upload')[0].files[0];
    //var img = new Image(), url = img.src = get_url;
    ////clearInputFile($('#banner-pic-upload')[0]);
    //var $img = $(img);
    //
    //var sleep = true;
    //
    //img.onerror = function() {
    //    //URL.revokeObjectURL(url)
    //    //$('#banner-pic').empty().append($img);
    //    //debugger;
    //    sleep = false;
    //    get_url = "";
    //};
    //img.onload = function() {
    //    sleep = false;
    //};

    //var i = setInterval(function() {
    //    if (sleep == false){
    //        clearInterval(i);
    //    }
    //}, 1000);


    //while (sleep){
    //
    //}
    //debugger;
    //sleep();
    //debugger;
    return get_url;
}

/**
 * 头像名字取后两位
 * 创建人:王杰
 * 周六 1.17
 */

function avatar_name(name) {
    var avatar_name = "";
    if (name.length > 2) {
        avatar_name = name.substr(name.length - 2);
    } else {
        avatar_name = name;
    }
    return avatar_name;
}
//
///**
// * 点击查看大图
// * 创建人:王杰
// * 周六 1.14
// */
//function watchBigPic(ob){
//    $(document).on('click','.'+ob+'',function(){
//        $('.big_img').remove();
//        var img_src = $(this).attr('src');
//
//        var call_back = document.createElement('div');
//        call_back.setAttribute('class','img_back');
//        call_back.style.position = 'fixed';
//        call_back.style.zIndex = '9999';
//        call_back.style.width = '100vw';
//        call_back.style.height = '100vh';
//        call_back.style.textAlign = 'center';
//        call_back.style.background = 'rgba(0, 0, 0, 0.4)';
//
//        var img = document.createElement('img');
//        img.src = img_src;
//        img.setAttribute('class','big_img');
//        img.style.marginLeft = 'auto';
//        img.style.marginRight = 'auto';
//
//        call_back.appendChild(img);
//        document.documentElement.appendChild(call_back);
//
//        var width = $('.big_img').css('width');
//        var height = $('.big_img').css('height');
//        var ht = parseInt(height.split("px")[0]);
//        var wd = parseInt(width.split("px")[0]);
//
//        if(wd>1090||ht>1920){
//            $('.big_img').css('width',(wd*2/3)/10.8+"vw");
//            $('.big_img').css('height',(ht*2/3)/19.2+"vh");
//        }else{
//            $('.big_img').css('width',wd/10.8+"vw");
//            $('.big_img').css('height',ht/19.2+"vh");
//        }
//        var marginTop = ((1920-ht)/2);
//        $('.big_img').css('marginTop',marginTop/19.2+"vh");
//
//        //点击移除隐藏
//        $('.img_back').on('click',function(){
//            $(this).remove();
//        });
//    });
//
//}


function convertCurrency(money) {
    //汉字的数字
    var cnNums = new Array('零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖');
    //基本单位
    var cnIntRadice = new Array('', '拾', '佰', '仟');
    //对应整数部分扩展单位
    var cnIntUnits = new Array('', '万', '亿', '兆');
    //对应小数部分单位
    var cnDecUnits = new Array('角', '分', '毫', '厘');
    //整数金额时后面跟的字符
    var cnInteger = '整';
    //整型完以后的单位
    var cnIntLast = '元';
    //最大处理的数字
    var maxNum = 999999999999999.9999;
    //金额整数部分
    var integerNum;
    //金额小数部分
    var decimalNum;
    //输出的中文金额字符串
    var chineseStr = '';
    //分离金额后用的数组，预定义
    var parts;
    if (money == '') {
        return '';
    }
    money = parseFloat(money);
    if (money >= maxNum) {
        //超出最大处理数字
        return '';
    }
    if (money == 0) {
        chineseStr = cnNums[0] + cnIntLast + cnInteger;
        return chineseStr;
    }
    //转换为字符串
    money = money.toString();
    if (money.indexOf('.') == -1) {
        integerNum = money;
        decimalNum = '';
    } else {
        parts = money.split('.');
        integerNum = parts[0];
        decimalNum = parts[1].substr(0, 4);
    }
    //获取整型部分转换
    if (parseInt(integerNum, 10) > 0) {
        var zeroCount = 0;
        var IntLen = integerNum.length;
        for (var i = 0; i < IntLen; i++) {
            var n = integerNum.substr(i, 1);
            var p = IntLen - i - 1;
            var q = p / 4;
            var m = p % 4;
            if (n == '0') {
                zeroCount++;
            } else {
                if (zeroCount > 0) {
                    chineseStr += cnNums[0];
                }
                //归零
                zeroCount = 0;
                chineseStr += cnNums[parseInt(n)] + cnIntRadice[m];
            }
            if (m == 0 && zeroCount < 4) {
                chineseStr += cnIntUnits[q];
            }
        }
        chineseStr += cnIntLast;
    }
    //小数部分
    if (decimalNum != '') {
        var decLen = decimalNum.length;
        for (var i = 0; i < decLen; i++) {
            var n = decimalNum.substr(i, 1);
            if (n != '0') {
                chineseStr += cnNums[Number(n)] + cnDecUnits[i];
            }
        }
    }
    if (chineseStr == '') {
        chineseStr += cnNums[0] + cnIntLast + cnInteger;
    } else if (decimalNum == '') {
        chineseStr += cnInteger;
    }
    return chineseStr;
}


var imgExt = new Array(".gif", ".jpg", ".jpeg", ".png", ".bmp", ".psd", ".dwg", ".ico");//图片文件的后缀名
var fileExt = new Array(".gif", ".jpg", ".jpeg", ".png", ".bmp", ".psd", ".dwg", ".ico", ".rar", ".zip", ".7z", ".exe", ".3gp", ".flv", ".mp3", ".wav", ".dat", ".swf", ".krc", ".lrc", ".wpc", ".wps", ".dps", ".et", ".txt", ".doc", ".docx", ".xls", ".xlsx", ".ppt", ".pptx", ".pdf", ".vsd", ".xmind", ".mmap", ".mpp", ".ceb", ".rp", ".apk", ".chm", ".sql", ".log", ".html", ".htm", ".xml");//支持文件的后缀名
function checkImageType(fileName) {
    var ext = "";
    var name = fileName.toLowerCase();
    var i = name.lastIndexOf(".");
    if (i > -1) {
        ext = name.substring(i);
    }

    for (var j = 0; j < imgExt.length; j++) {
        if (imgExt[j] === ext)
            return true;
    }
    return false;
}

function checkFileType(fileName) {
    var ext = "";
    var name = fileName.toLowerCase();
    var i = name.lastIndexOf(".");
    if (i > -1) {
        ext = name.substring(i);
    }

    for (var j = 0; j < fileExt.length; j++) {
        if (fileExt[j] === ext)
            return true;
    }
    return false;
}


//input text 过滤
function HTMLEncode(html) {
    //console.log(html);
    if (html == undefined) {
        return html;
    }
    //html = (stri)
    html = html.replace(/</g, "&lt;");
    html = html.replace(/>/g, "&gt;");
    return html;
}

function INPUTEncode(html) {
    if (html == undefined) {
        return html;
    }
    html = html.replace(/&lt;/g, '<');
    html = html.replace(/&gt;/g, '>');
    return html;
}

//function HTMLUncode(html) {
//    //html = $("<span />", { html: html }).text();
//    //
//    //return HTMLEncode(html);
//    //debugger;
//
//    var strings = html.split("&lt;");
//
//    var response = "";
//    for (var i = 0; i < strings.length; i ++){
//        response = response + strings[i];
//        if (i < strings.length - 1){
//            response = response + "<";
//        }
//    }
//    response = response.replace("<script","&lt;script");
//    response = response.replace("</script","&lt;/script");
//
//    //debugger;
//    //response = HTMLEncode(response);
//
//    //html = html.replace("&lt;",/[<]/g);
//    //html = html.replace("&lt;div>","<div>");
//    //html = html.replace("</script>","&lt;/script&gt;");
//    return response;
//
//    //var temp = document.createElement("div");
//    //(temp.textContent != null) ? (temp.textContent = html) : (temp.innerText = html);
//    //var output = temp.innerHTML;
//    //temp = null;
//    //return output;
//}


//时间戳转换
function timestampToString(timestamp, formatter) {
    //获得时间
    var date = new Date(timestamp);
    //解析
    var tempTime = {
        year: date.getFullYear(),
        month: (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1),
        day: date.getDate() < 10 ? '0' + date.getDate() : date.getDate(),
        hours: date.getHours() < 10 ? '0' + date.getHours() : date.getHours(),
        min: date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes(),
        sec: date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds()
    };
    //定义类型
    var formatters =
        [
            "yyyy-MM-dd",
            "yyyy-MM-dd HH:mm",
            "yyyy-MM-dd HH:mm:ss",
            "MM-dd", "MM-dd HH:mm",
            "MM-dd HH:mm:ss",
            "yyyy.MM.dd",
            "yyyy.MM.dd HH:mm",
            "yyyy.MM.dd HH:mm:ss",
            "MM.dd",
            "MM.dd HH:mm",
            "MM.dd HH:mm:ss",
            "yyyy/MM/dd",
            "yyyy/MM/dd HH:mm",
            "yyyy/MM/dd HH:mm:ss",
            "MM/dd", "MM/dd HH:mm",
            "MM/dd HH:mm:ss",
            "yyyy年MM月dd日",
            "yyyy年MM月dd日 HH:mm",
            "yyyy年MM月dd日 HH:mm:ss",
            "MM月dd日",
            "MM月dd日 HH:mm",
            "MM月dd日 HH:mm:ss",
            "HH:mm",
            "HH:mm:ss",
            "mm:ss"
        ];

    //获得类型编号
    var index = formatters.indexOf(formatter);
    //换算
    var returnString = "";
    switch (index) {
        case 0:
            returnString = tempTime.year + "-" + tempTime.month + "-" + tempTime.day;
            break;
        case 1:
            returnString = tempTime.year + "-" + tempTime.month + "-" + tempTime.day + " " + tempTime.hours + ":" + tempTime.min;
            break;
        case 2:
            returnString = tempTime.year + "-" + tempTime.month + "-" + tempTime.day + " " + tempTime.hours + ":" + tempTime.min + ":" + tempTime.sec;
            break;
        case 3:
            returnString = tempTime.month + "-" + tempTime.day;
            break;
        case 4:
            returnString = tempTime.month + "-" + tempTime.day + " " + tempTime.hours + ":" + tempTime.min;
            break;
        case 5:
            returnString = tempTime.month + "-" + tempTime.day + " " + tempTime.hours + ":" + tempTime.min + ":" + tempTime.sec;
            break;
        case 6:
            returnString = tempTime.year + "." + tempTime.month + "." + tempTime.day;
            break;
        case 7:
            returnString = tempTime.year + "." + tempTime.month + "." + tempTime.day + " " + tempTime.hours + ":" + tempTime.min;
            break;
        case 8:
            returnString = tempTime.year + "." + tempTime.month + "." + tempTime.day + " " + tempTime.hours + ":" + tempTime.min + ":" + tempTime.sec;
            break;
        case 9:
            returnString = tempTime.month + "." + tempTime.day;
            break;
        case 10:
            returnString = tempTime.month + "." + tempTime.day + " " + tempTime.hours + ":" + tempTime.min;
            break;
        case 11:
            returnString = tempTime.month + "." + tempTime.day + " " + tempTime.hours + ":" + tempTime.min + ":" + tempTime.sec;
            break;
        case 12:
            returnString = tempTime.year + "/" + tempTime.month + "/" + tempTime.day;
            break;
        case 13:
            returnString = tempTime.year + "/" + tempTime.month + "/" + tempTime.day + " " + tempTime.hours + ":" + tempTime.min;
            break;
        case 14:
            returnString = tempTime.year + "/" + tempTime.month + "/" + tempTime.day + " " + tempTime.hours + ":" + tempTime.min + ":" + tempTime.sec;
            break;
        case 15:
            returnString = tempTime.month + "/" + tempTime.day;
            break;
        case 16:
            returnString = tempTime.month + "/" + tempTime.day + " " + tempTime.hours + ":" + tempTime.min;
            break;
        case 17:
            returnString = tempTime.month + "/" + tempTime.day + " " + tempTime.hours + ":" + tempTime.min + ":" + tempTime.sec;
            break;
        case 18:
            returnString = tempTime.year + "年" + tempTime.month + "月" + tempTime.day + "日";
            break;
        case 19:
            returnString = tempTime.year + "年" + tempTime.month + "月" + tempTime.day + "日" + " " + tempTime.hours + ":" + tempTime.min;
            break;
        case 20:
            returnString = tempTime.year + "年" + tempTime.month + "月" + tempTime.day + "日" + " " + tempTime.hours + ":" + tempTime.min + ":" + tempTime.sec;
            break;
        case 21:
            returnString = tempTime.month + "月" + tempTime.day + "日";
            break;
        case 22:
            returnString = tempTime.month + "月" + tempTime.day + "日" + " " + tempTime.hours + ":" + tempTime.min;
            break;
        case 23:
            returnString = tempTime.month + "月" + tempTime.day + "日" + " " + tempTime.hours + ":" + tempTime.min + ":" + tempTime.sec;
            break;
        case 24:
            returnString = tempTime.hours + ":" + tempTime.min;
            break;
        case 25:
            returnString = tempTime.hours + ":" + tempTime.min + tempTime.sec;
            break;
        case 26:
            returnString = tempTime.min + ":" + tempTime.sec;
            break;
        default:
            //默认
            returnString = tempTime.year + "-" + tempTime.month + "-" + tempTime.day + " " + tempTime.hours + ":" + tempTime.min;
            break;
    }
    //console.log(returnString);
    //返回
    return returnString;
}

//url识别

function hrefComplex(href){
    href = href.toLocaleString();

    var prefix_http = "http://";
    prefix_http = prefix_http.toLocaleLowerCase();
    var prefix_https = "https://";
    prefix_https = prefix_https.toLocaleLowerCase();

    if (href.length > 7){
        if (href.substring(0,7) != prefix_http && href.substring(0,8) != prefix_https){
            href = "http://" + href;
        }
    }else {
        href = "http://" + href;
    }

    return href;

}

